#include "uartprocessor.h"
#include <QDebug>
#include <QCoreApplication>
uartProcessor::uartProcessor(QObject *parent,QMainWindow *w) : QObject(parent)
{
    int baud,check,data,stop;
    parentMainWindow = w;
    selectWindow = new UartSelect();
    selectWindow->move(w->x()+200,w->y()+200);
    selectWindow->exec();
    QString st = selectWindow->selectedUartName;
    baud = selectWindow->selectedBaud;
    check = selectWindow->selectedCheck;
    data = selectWindow->selectedData;
    stop = selectWindow->selectedStop;

    delete selectWindow;

    if (st=="NotSelected")
    {
        QMessageBox::information(NULL,"注意","未选择串口","确定");
        Status = -1;
        return;
    }
    Uart = new QSerialPort(st);
    if(!Uart->open(QIODevice::ReadOnly))
    {
        QMessageBox::warning(NULL,"错误","串口无法打开","确定");
        Status = -1;
        return;
    }
    const QSerialPort::BaudRate baudList[8] = {QSerialPort::Baud1200,QSerialPort::Baud2400,
                           QSerialPort::Baud4800,QSerialPort::Baud9600,
                           QSerialPort::Baud19200,QSerialPort::Baud38400,
                           QSerialPort::Baud57600,QSerialPort::Baud115200};
    Uart->setBaudRate(baudList[baud]);
    const QSerialPort::Parity checkList[5] = {QSerialPort::NoParity,QSerialPort::EvenParity,
                             QSerialPort::OddParity,QSerialPort::SpaceParity,
                             QSerialPort::MarkParity};
    Uart->setParity(checkList[check]);
    const QSerialPort::StopBits stopList[3]  ={QSerialPort::OneStop,QSerialPort::OneAndHalfStop,
                           QSerialPort::TwoStop};
    Uart->setStopBits(stopList[stop]);
    const QSerialPort::DataBits dataList[4] = {QSerialPort::Data5,QSerialPort::Data6,
                            QSerialPort::Data7,QSerialPort::Data8};
    Uart->setDataBits(dataList[data]);
    Uart->setFlowControl(QSerialPort::NoFlowControl);

    Uart->clearError();
    Uart->clear();
    connect(Uart,SIGNAL(readyRead()),this,SLOT(getData()));
    receiveFile = new QFile(QCoreApplication::applicationDirPath()+"//receiveText.txt");
    receiveFile->open(QIODevice::WriteOnly);

    readable = false;
    countStart = 0;
    countRestart = 0;
    countEnd = 0;
    tempCount = 0;

    Status = 0;
}
void uartProcessor::setMapSize(int w,int h)
{
    this->Width = w;
    this->Height = h;
    leftEdge = Width*10;
    rightEdge = Height*10;
    leftMain = Height;
    rightMain = Height;
    midMain = Height;
    extraData =55;
}

uartProcessor::~uartProcessor()
{
    if (Status==0)
    {
        Uart->close();
        receiveFile->close();

    }
}

void uartProcessor::setTargetData(QPixmap *tmap, DataSheet *tsheet, long long *tdata)
{
    targetMap = tmap;
    targetDataSheet = tsheet;
    targetOnlineData = tdata;
}
void uartProcessor::getData()
{
    Data= Uart->readAll();
    receiveFile->write(Data);
    unsigned char ch;
    for (int i=0;i<Data.length();i++)
    {
        ch = (unsigned char)Data[i];
        if (readable)
        {
            tempCount++;
            tempData[tempCount] = ch;
            if (ch==0x0f)
            {
                if (countEnd==5)
                {
                    readable = false;

                    countEnd = 0;
                    if (tempCount>=1380)
                    {
                        mapGenerate();
                        emit done();
                    }
                    tempCount = 0;
                }
            }
            if (ch==0xaa)
                countEnd++;
            else
                countEnd = 0;
        }
        else
        {
            if (ch==0xf0)
            {
                if (countStart==5)
                {
                    readable = true;
                    tempCount = 0;
                    countStart = 0;
                }
            }
            if (ch==0xaa)
                countStart++;
            else
                countStart = 0;
        }
    }
}
void uartProcessor::mapGenerate()
{
    for (int i=0;i<Width;i++)
        for (int j=0;j<Height;j++)
            mapData[i][j] = 0;
    for (int i=1;i<=Height;i++)
        for (int j=1;j<=10;j++)
        {
            if ((tempData[(i-1)*10+j]==0)||(tempData[10*Height+(i-1)*10+j]==0))
                break;
            for (int k = tempData[(i-1)*10+j]-1;k<=tempData[(i-1)*10+j+10*Height]-1;k++)
                mapData[k][i-1]=1;
           targetDataSheet->changeDataItem(2*(i-1),j-1,tempData[(i-1)*10+j]);
           targetDataSheet->changeDataItem(2*i-1,j-1,tempData[(i-1)*10+j+10*Height]);
        }
    //1200
    static const QRgb rgb[2] = {qRgb(0,0,0),qRgb(255,255,255)};
    for (int i=0;i<Width;i++)
        for (int j=0;j<Height;j++)
           targetImg.setPixel(i,j,rgb[mapData[i][j]]);
    //60
    for (int i=0;i<Height;i++)//左边界
    {

        if (tempData[1+leftEdge+rightEdge+i]>0)
        {
            targetImg.setPixel(tempData[1+leftEdge+rightEdge+i]-1,i,*lAv);
            targetDataSheet->changeDataItem(2*i,10,tempData[1+leftEdge+rightEdge+i]);
        }
    }
    //60
    for (int i=0;i<Height;i++) //右边界
        if (tempData[1+leftEdge+rightEdge+leftMain+i]>0)
        {
            targetImg.setPixel(tempData[1+leftEdge+rightEdge+leftMain+i]-1,i,*rAv);
            targetDataSheet->changeDataItem(2*i+1,11,tempData[1+leftEdge+rightEdge+leftMain+i]);
        }
    //中线
    //60
    for (int i=0;i<Height;i++)
        if (tempData[1+leftEdge+rightEdge+leftMain+rightMain+i]>0)
        {
            targetImg.setPixel(tempData[1+leftEdge+rightEdge+leftMain+rightMain+i]-1,i,*mAv);
            targetDataSheet->changeDataItem(2*(i),12,tempData[1+leftEdge+rightEdge+leftMain+rightMain+i]);
        }
    //存储相关数据
    targetOnlineData[1] = tempData[leftEdge+rightEdge+leftMain+rightMain+midMain+1];
    targetOnlineData[2] = tempData[leftEdge+rightEdge+leftMain+rightMain+midMain+2];


    *targetMap = QPixmap::fromImage(targetImg);
}

